#include "Weapon.h"
#include "Bullet.h"
#include "BaseObject.h"
#include "BulletFactory.h"
#include "BulletsBank.h"
#include "CNumber.h"
#include "MainLayer.h"
#include "ObjectID.h"


Weapon::Weapon()
{
	_timeDelay = 0.0f;
	_listBulletsData = NULL;
	_id = NULL;
	_extraData = NULL;
}


Weapon::~Weapon(void)
{
	CC_SAFE_RELEASE(_listBulletsData);
	CC_SAFE_RELEASE(_id);
	CC_SAFE_RELEASE(_extraData);
}

CCArray* Weapon::addBullet()
{
	CCArray* listBullet = CCArray::create();
 	BaseObject* _owner = getOwner();
 	//CCPoint pos = ccp(_owner->boundingBox().getMidX(), _owner->boundingBox().getMidY());

	ObjectID* objId = ObjectID::create();

 	CCObject* it;
 	CCARRAY_FOREACH(_listBulletsData, it)
 	{
 		CCDictionary* bulletData = dynamic_cast<CCDictionary*>(it);
 
 		char* bId = (char*)dynamic_cast<CCString*>(bulletData->objectForKey("Id"))->getCString();
 		BulletFactory* bFactory = dynamic_cast<BulletFactory*>(BulletsBank::sharedBulletBank()->getBulletFactories()->objectForKey(bId));
 
 		CCPoint position = dynamic_cast<CCNumber<CCPoint>*>(bulletData->objectForKey("Position"))->getNumber();
 		CCPoint velocity = dynamic_cast<CCNumber<CCPoint>*>(bulletData->objectForKey("Velocity"))->getNumber();
 		CCPoint scale = dynamic_cast<CCNumber<CCPoint>*>(bulletData->objectForKey("Scale"))->getNumber();
		int damage = dynamic_cast<CCNumber<int>*>(bulletData->objectForKey("Damage"))->getNumber();
 
 		Bullet* bullet = bFactory->createBullet();

		
 		bullet->setPosition(position);
 		bullet->setVelocity(velocity);
 		bullet->setScaleX(scale.x);
 		bullet->setScaleY(scale.y);
		bullet->setIdWave(objId);
		bullet->setDamage(damage);
 
 		// Add bullet to MainLayer
 		_owner->shoot(bullet);
		listBullet->addObject(bullet);
 	}

	return listBullet;
}

void Weapon::removeBullet( Bullet* bullet )
{
}

bool Weapon::init()
{
	if (!CCNode::init())
		return false;

	return true;
}


/* Get set for Owner */
BaseObject* Weapon::getOwner()
{
	return dynamic_cast<BaseObject*>(getParent()->getParent());
}


void Weapon::processsing( float dt )
{

}

void Weapon::onEnter()
{
	CCNode::onEnter();
}

void Weapon::onExit()
{
	CCNode::onExit();
}


